package com.erp.module.packPiece

import android.app.Activity
import androidx.appcompat.app.AlertDialog
import android.content.Intent
import android.view.View
import com.erp.module.packPiece.packPieceGather.PackPieceGatherAvtivity
import com.erp.myAdapter.ArrayAdapter
import com.erp.myAdapter.PackPieceKAdapter
import com.erp.bean.PackPieceBean
import com.erp.mclass.QuanXian
import com.erp.R
import com.erp.util.Dialogutil
import com.erp.util.Myutil
import com.erp.view.PullToRefreshView
import com.erp.myFragment.BaseKFragment
import kotlinx.android.synthetic.main.fragment_packpiece.*

/**
 * Created by Administrator on 2018/12/14/014.
 * 作者：刘洋
 * 拼装包装计件采集Fragment
 */
class FragmentPackPiece : BaseKFragment(), PackPieceContract.PackPieceFragmentView, View.OnClickListener, PullToRefreshView.OnHeaderRefreshListener, PullToRefreshView.OnFooterRefreshListener {
    override fun getGonghaoOK(gonghaoid: MutableList<String>, gonghaoname: MutableList<String>) {
        this.gonghao = gonghaoid
        this.gonghaoname = gonghaoname
        val av = ArrayAdapter(context, android.R.layout.simple_dropdown_item_1line, gonghaoname)
        AtGongHao.setAdapter(av)
    }

    override fun getGonghaoNO() {
    }

    lateinit var gonghao: MutableList<String>
    lateinit var gonghaoname: MutableList<String>
    lateinit var KP : PackPieceContract.PackPieceKP
    var updialog: AlertDialog?=null
    lateinit var sousuo : SousuoBean

    lateinit var adapter : PackPieceKAdapter
    lateinit var mlist : MutableList<PackPieceBean>
    override fun getLayoutId(): Int {
        return R.layout.fragment_packpiece
    }

    override fun initData() {
        gonghaoname = java.util.ArrayList()
        gonghao = java.util.ArrayList()
        mlist=ArrayList()
        KP = PackPieceP(mlist, this, context!!)
        adapter= PackPieceKAdapter(mlist, context!!)
        LvPackPiece.adapter=adapter
        sousuo= SousuoBean()
        Pull.setheader(true)
        Pull.setOnFooterRefreshListener(this)
        Pull.setOnHeaderRefreshListener(this)
        LlShangChuan.setOnClickListener(this)
        BtGuoLv.setOnClickListener(this)
        TvStartTime.setOnClickListener(this)
        TvEndTime.setOnClickListener(this)
        AtGongHao.setOnClickListener { AtGongHao.showDropDown() }
//        updialog = Myutil.showupdatedialog(activity, "正在获取纸箱数据", false)
        Pull.headerRefreshing()
        KP.getPackPiece(sousuo)
        KP.getGonghao()
        if(!QuanXian.isEditPackPiece){
            LlShangChuan.visibility=View.GONE
        }
    }

    fun  qingkong(){
        if(isFirstLoad){
            return
        }
        Pull.headerRefreshing()
    }

    override fun onClick(v: View?) {
        when(v){
            LlShangChuan -> {
                val i = Intent(activity, PackPieceGatherAvtivity:: class.java)
                startActivityForResult(i,3)
            }
            BtGuoLv -> {
                updialog = Myutil.showupdatedialog(activity, "正在查询", false)
                sousuo.start_time=TvStartTime.text.toString()
                sousuo.end_time=TvEndTime.text.toString()
                for (i in gonghaoname.indices) {
                    if (gonghaoname[i] == AtGongHao.text.toString().trim { it <= ' ' }) {
                        sousuo.name_id= gonghao[i]
                        continue
                    }
                }
                if(!gonghaoname.contains(AtGongHao.text.toString())){
                    sousuo.name_id=""
                }
                KP.getPackPiece(sousuo)
            }
            TvStartTime -> {
                Dialogutil.showBottoPopupWindow(TvStartTime, "选择开始时间", TvStartTime.text.toString(), activity)
                Myutil.notjianpan(context, ivs)
            }
            TvEndTime -> {
                Dialogutil.showBottoPopupWindow(TvEndTime, "选择结束时间", TvEndTime.text.toString(), activity)
                Myutil.notjianpan(context, ivs)
            }
        }
    }

    override fun onFooterRefresh(view: PullToRefreshView?) {
        KP.addPackPiece()

    }

    override fun onHeaderRefresh(view: PullToRefreshView?) {
        KP.getPackPiece(sousuo)
    }

    override fun getPackPieceOk(name : String , counts :String,gangwei_name:String) {
        if(name!=""){
            LlTongji.visibility=View.VISIBLE
            TvName.text=name
            TvCount.text=counts
            TvGangwei.text=gangwei_name
        }else{
            LlTongji.visibility=View.GONE
        }
        updialog?.dismiss()
        adapter.notifyDataSetChanged()
        Pull.onHeaderRefreshComplete()
    }

    override fun getPackPieceNo() {
        updialog?.dismiss()
        Pull.onHeaderRefreshComplete()
    }

    override fun addPackPieceOk() {
        Pull.onFooterRefreshComplete()
        adapter.notifyDataSetChanged()
    }

    override fun addPackPieceNo() {
        Pull.onFooterRefreshComplete()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(data==null||resultCode!= Activity.RESULT_OK){
            return
        }
        when(requestCode) {
            3 -> {
                updialog = Myutil.showupdatedialog(activity, "正在获取纸箱数据", false)
                KP.getPackPiece(sousuo)
            }
        }
    }

    class SousuoBean{
        var name_id = ""
        var start_time =""
        var end_time =""
        var time_category ="pp.ctime"
        override fun toString(): String {
            return "SousuoBean(name_id='$name_id', start_time='$start_time', end_time='$end_time', time_category='$time_category')"
        }

    }

}